<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/19
 * Time: 上午9:23
 */
namespace app\admin\controller;

use app\common\model\Settings;
use think\Exception;
use think\Db;

class Content extends Common
{
    //分类列表
    public function category()
    {
        //标题
        $this->templateTitle('新闻分类管理');

        //判断条件
        $where=[];

        //搜索条件
        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["title"] = ["LIKE", "%$condition%"];
        }

        //获取新闻分类
        $list = Db::name("content_category")
            ->where($where)
            ->order("display_order DESC,update_time DESC")
            ->paginate(10);
        $this->assign("list", $list);

        return $this->fetch();
    }

    //添加分类
    public function addCategory()
    {
        if (request()->isGet())
        {
            $this->layoutModal();

            return $this->fetch("content/category/add");
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "create_time" => now_datetime(),
                "update_time" => now_datetime(),
            );
            //分类名称
            $data["title"] = request()->param("title");
            //分类描述
            $data["description"] = request()->param("desc");
            //分类排序
            $data["display_order"] = request()->param("order");
            //是否显示
            $data["is_visible"] = request()->param("visible");

            try
            {
                $add = Db::name("content_category")->insert($data);
                if($add){
                    $this->ajaxSuccess('分类添加成功');
                }else{
                    $this->ajaxError('分类添加失败');
                }

            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //编辑新闻分类
    public function editCategory()
    {
        //分类ID
        $categoryId = request()->param("cid");

        if (request()->isGet())
        {
            $this->layoutModal();

            //获取分类信息
            $category = Db::name("content_category")
                ->where("id", $categoryId)
                ->find();
            $this->assign("category", $category);

            return $this->fetch("content/category/edit");
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //分类名称
            $data["title"] = request()->param("title");
            //分类描述
            $data["description"] = request()->param("desc");
            //分类排序
            $data["display_order"] = request()->param("order");
            //是否显示
            $data["is_visible"] = request()->param("visible");

            try
            {
                //更新分类ID路径
                $save = Db::name("content_category")
                    ->where("id", $categoryId)
                    ->update($data);
                if($save){
                    $this->ajaxSuccess('编辑新闻分类成功');
                }else{
                    $this->ajaxError('编辑新闻分类失败');
                }

            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //删除新闻分类
    public function removeCategory()
    {
        if (request()->isAjax())
        {
            //分类ID
            $categoryId = request()->param("cid");

            try
            {
                //更新分类ID路径
                $del = Db::name("content_category")
                    ->where("id", $categoryId)
                    ->delete();
                if($del){
                    $this->ajaxSuccess('新闻分类删除成功');
                }else{
                    $this->ajaxError('新闻分类删除失败');
                }

            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //新闻列表
    public function article()
    {
        $this->templateTitle('链新闻');

        //新闻分类列表
        $categoryList = $this->categoryList();
        $this->assign("category_list", $categoryList);
        //判断条件
        $where['ca.id']=['gt',0];

        //搜索条件-省市
        $pro = trim(input('pro'));
        $this->assign('area_province',$pro);
        if($pro){
            $where['ca.pro'] = $pro;
        }

        $city = trim(input('city'));
        $this->assign('area_city',$city);
        if($city){
            $where['ca.city'] = $city;
        }

        //分类条件
        $categoryId = request()->param("cid");
        $this->assign("cid", $categoryId);
        if ($categoryId)
        {
            $where["ca.category_id"] = ["EQ", $categoryId];
        }

        $bussiness_name = request()->param("bussiness_name");
        $this->assign("bussiness_name", $bussiness_name);
        if ($bussiness_name)
        {
            $where["bb.title|bb.id"] = $bussiness_name;
        }

        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["ca.title"] = ["LIKE", "%$condition%"];
        }

        //获取新闻分类
        $list = Db::name("content_article")
            ->alias("ca")
            ->join("__CONTENT_CATEGORY__ cc", "cc.id=ca.category_id",'LEFT')
            ->join("__BUSSINESS__ bb", "bb.id=ca.bussiness_id",'LEFT')
            ->where($where)
            ->order("ca.display_order DESC,ca.update_time DESC")
            ->field("ca.*,cc.title AS category_name,bb.title as bussiness_name")
            ->paginate(10);
        $this->assign("list", $list);

        return $this->fetch();
    }

    //添加新闻
    public function addArticle()
    {
        if (request()->isGet())
        {
            $this->layoutModal();

            //新闻分类列表
            $categoryList = $this->categoryList();
            $this->assign("category_list", $categoryList);
            $this->assign("tag_list", get_article_tag('',2));
            return $this->fetch("content/article/add");
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "create_time" => now_datetime(),
                "update_time" => now_datetime(),
            );
            //分类ID
            $data["category_id"] = request()->param("cid");
            $data["tag_id"] = request()->param("tag_id");
            //新闻名称
            $data["title"] = request()->param("title");
            //新闻缩略图
            $data["thumb"] = request()->param("thumb");
            if(empty($data['thumb'])){
                return $this->ajaxError('请上传图片');
            }
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }
            //新闻描述
            $data["description"] = request()->param("desc");
            //新闻排序
            $data["display_order"] = request()->param("order");
            //浏览量
            $data["view_num"] = request()->param("view_num");
            //是否显示
            $data["is_visible"] = request()->param("visible");
            //是否首页推荐
            $data["is_recommend"] = request()->param("recommend");
            //新闻内容
            $detail = request()->param("article_detail");
            $data["detail"] = $detail;

            try
            {
                Db::name("content_article")->insert($data);

                $this->ajaxSuccess('新闻添加成功');
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //编辑新闻
    public function editArticle()
    {
        //新闻ID
        $articleId = request()->param("aid");

        if (request()->isGet())
        {
            $this->layoutModal();

            //新闻分类列表
            $categoryList = $this->categoryList();
            $this->assign("category_list", $categoryList);
            $this->assign("tag_list", get_article_tag('',2));
            //获取新闻信息
            $article = Db::name("content_article")
                ->alias("ca")
                ->join("__BUSSINESS__ bb", "bb.id=ca.bussiness_id",'LEFT')
                ->where("ca.id", $articleId)
                ->field("ca.*,bb.title as bussiness_name")
                ->find();
            $this->assign("article", $article);

            //处理缩略图
            $this->assign("single_image", array("thumb"=>$article["thumb"]));
            //处理详细内容
            $this->assign("ueditor_content", array("article_detail"=>$article["detail"]));
            $this->assign("area_province", $article['pro']);
            $this->assign("area_city", $article['city']);

            return $this->fetch("content/article/edit");
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //分类ID
            $data["category_id"] = request()->param("cid");
            $data["tag_id"] = request()->param("tag_id");
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }
            //新闻名称
            $data["title"] = request()->param("title");
            //新闻缩略图
            $data["thumb"] = request()->param("thumb");
            //新闻描述
            $data["description"] = request()->param("desc");
            //新闻排序
            $data["display_order"] = request()->param("order");
            //浏览量
            $data["view_num"] = request()->param("view_num");
            //是否显示
            $data["is_visible"] = request()->param("visible");
            //是否首页推荐
            $data["is_recommend"] = request()->param("recommend");
            //新闻内容
            $detail = request()->param("article_detail");
            $data["detail"] = $detail;

            try
            {
                //更新新闻
                Db::name("content_article")
                    ->where("id", $articleId)
                    ->update($data);

                $this->ajaxSuccess('新闻编辑成功');
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //推荐新闻
    public function recommendArticle()
    {
        if (request()->isAjax())
        {
            //新闻ID
            $articleId = request()->param("aid");
            //推荐状态
            $status = request()->param("status");

            //更新新闻
            Db::name("content_article")
                ->where("id", $articleId)
                ->update(array(
                    "is_recommend" => $status,
                    "update_time" => now_datetime(),
                ));

            $this->ajaxSuccess('推荐状态修改成功');
        }
    }

    //删除新闻
    public function removeArticle()
    {
        if (request()->isAjax())
        {
            //新闻ID
            $articleId = request()->param("aid");

            try
            {
                //更新新闻
                Db::name("content_article")
                    ->where("id", $articleId)
                    ->delete();

                $this->ajaxSuccess('新闻删除成功');
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //新闻分类列表
    private function categoryList()
    {
        $list = Db::name("content_category")
            ->where("is_visible",1)
            ->order("display_order DESC,create_time DESC")
            ->field("id,title")
            ->select();

        return empty($list) ? array() : $list;
    }

    //关于我们
    public function aboutUs()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("关于我们");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_ABOUT_US);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //帮助内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_ABOUT_US, $content);

            $this->ajaxSuccess("设置成功！");
        }
    }

    //联系我们
    public function contectUs()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("联系我们");

            //设置内容
            $settings = Settings::getSettings("key_contect_us");
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //帮助内容
            $content = input("content");

            Settings::setSettings("key_contect_us", $content);

            $this->ajaxSuccess("设置成功！");
        }
    }

    //服务协议
    public function treaty()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("服务协议");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_TREATY);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //协议内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_TREATY, $content);

            $this->ajaxSuccess("修改成功！");
        }
    }

    //卡券使用说明
    public function discountExplain()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("卡券使用说明");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_DISCOUNT_EXPLAIN);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //协议内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_DISCOUNT_EXPLAIN, $content);

            $this->ajaxSuccess("修改成功！");
        }
    }
}